<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>StopwatchCPU.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="StopwatchCPU code in Java">
<meta NAME="TITLE" CONTENT="StopwatchCPU code in Java">
<meta NAME="KEYWORDS" CONTENT="StopwatchCPU,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>StopwatchCPU.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "StopwatchCPU.java">StopwatchCPU.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac StopwatchCPU.java</span>
<span class="comment"> *  Execution:    java StopwtachCPU n</span>
<span class="comment"> *  Dependencies: none</span>
<span class="comment"> *</span>
<span class="comment"> *  A version of Stopwatch.java that measures CPU time on a single</span>
<span class="comment"> *  core or processor (instead of wall clock time).</span>
<span class="comment"> *</span>
<span class="comment"> *  % java8 StopwatchCPU 100000000</span>
<span class="comment"> *  6.666667e+11 (1.05 seconds)</span>
<span class="comment"> *  6.666667e+11 (7.50 seconds)</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">lang</span><span class="symbol">.</span><span class="normal">management</span><span class="symbol">.</span><span class="normal">ThreadMXBean</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">lang</span><span class="symbol">.</span><span class="normal">management</span><span class="symbol">.</span><span class="normal">ManagementFactory</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StopwatchCPU</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type is for measuring</span>
<span class="comment"> *  the CPU time used during a programming task.</span>
<span class="comment"> *</span>
<span class="comment"> *  See {</span><span class="type">@link</span><span class="comment"> Stopwatch} for a version that measures wall-clock time</span>
<span class="comment"> *  (the real time that elapses).</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Josh Hug</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>

<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">StopwatchCPU</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> NANOSECONDS_PER_SECOND </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1000000000</span><span class="symbol">;</span>

<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">ThreadMXBean</span><span class="normal"> threadTimer</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">long</span><span class="normal"> start</span><span class="symbol">;</span>
<span class="normal">            </span>
<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Initializes a new stopwatch.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">StopwatchCPU</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal">  </span>
<span class="normal">        threadTimer </span><span class="symbol">=</span><span class="normal"> ManagementFactory</span><span class="symbol">.</span><span class="function">getThreadMXBean</span><span class="symbol">();</span>
<span class="normal">        start </span><span class="symbol">=</span><span class="normal"> threadTimer</span><span class="symbol">.</span><span class="function">getCurrentThreadCpuTime</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span><span class="normal">   </span>
<span class="normal">        </span>
<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the elapsed CPU time (in seconds) since the stopwatch was created.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> elapsed CPU time (in seconds) since the stopwatch was created</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">elapsedTime</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">long</span><span class="normal"> now </span><span class="symbol">=</span><span class="normal"> threadTimer</span><span class="symbol">.</span><span class="function">getCurrentThreadCpuTime</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">now </span><span class="symbol">-</span><span class="normal"> start</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> NANOSECONDS_PER_SECOND</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StopwatchCPU</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     * Takes a command-line argument </span><span class="keyword">&lt;tt&gt;</span><span class="comment">n</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> and computes the </span>
<span class="comment">     * sum of the square roots of the first </span><span class="keyword">&lt;tt&gt;</span><span class="comment">n</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> positive integers,</span>
<span class="comment">     * first using </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Math.sqrt()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, then using </span><span class="keyword">&lt;tt&gt;</span><span class="comment">Math.pow()</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * It prints to standard output the sum and the amount of time to</span>
<span class="comment">     * compute the sum. Note that the discrete sum can be approximated by</span>
<span class="comment">     * an integral - the sum should be approximately 2/3 * (n^(3/2) - 1).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> n </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>

<span class="normal">        </span><span class="comment">// sum of square roots of integers from 1 to n using Math.sqrt(x).</span>
<span class="normal">        </span><span class="usertype">StopwatchCPU</span><span class="normal"> timer1 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">StopwatchCPU</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> sum1 </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;=</span><span class="normal"> n</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            sum1 </span><span class="symbol">+=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">sqrt</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> time1 </span><span class="symbol">=</span><span class="normal"> timer1</span><span class="symbol">.</span><span class="function">elapsedTime</span><span class="symbol">();</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%e (%.2f seconds)</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> sum1</span><span class="symbol">,</span><span class="normal"> time1</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// sum of square roots of integers from 1 to n using Math.pow(x, 0.5).</span>
<span class="normal">        </span><span class="usertype">StopwatchCPU</span><span class="normal"> timer2 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">StopwatchCPU</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> sum2 </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;=</span><span class="normal"> n</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            sum2 </span><span class="symbol">+=</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">pow</span><span class="symbol">(</span><span class="normal">i</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.5</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> time2 </span><span class="symbol">=</span><span class="normal"> timer2</span><span class="symbol">.</span><span class="function">elapsedTime</span><span class="symbol">();</span>
<span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%e (%.2f seconds)</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> sum2</span><span class="symbol">,</span><span class="normal"> time2</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>
<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
